package com.example.hello.controller;

import com.example.hello.common.Result;
import com.example.hello.entity.ReportSummary;
import com.example.hello.entity.EmployeeOrderStats;
import com.example.hello.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reports")
public class ReportController {

    private static final Logger log = LoggerFactory.getLogger(ReportController.class);

    @Autowired
    private ReportService reportService;
    
    @Autowired
    private DataSource dataSource;

    /**
     * 获取报表概览数据
     */
    @GetMapping("/summary")
    public Result<ReportSummary> getReportSummary(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        try {
            log.info("获取报表概览数据: startDate={}, endDate={}", startDate, endDate);
            ReportSummary summary = reportService.getReportSummary(startDate, endDate);
            return Result.success(summary);
        } catch (Exception e) {
            log.error("获取报表概览数据失败", e);
            return Result.error("获取报表概览数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取员工订单统计数据（排行榜）
     */
    @GetMapping("/employee-stats")
    public Result<List<EmployeeOrderStats>> getEmployeeOrderStats(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @RequestParam(required = false, defaultValue = "5") Integer limit) {
        
        try {
            log.info("获取员工订单统计数据: startDate={}, endDate={}, limit={}", startDate, endDate, limit);
            List<EmployeeOrderStats> stats = reportService.getEmployeeOrderStats(startDate, endDate, limit);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取员工订单统计数据失败", e);
            return Result.error("获取员工订单统计数据失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取完整报表数据（概览数据和员工统计数据）
     */
    @GetMapping("/dashboard")
    public Result<Map<String, Object>> getReportDashboard(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @RequestParam(required = false, defaultValue = "5") Integer limit) {
        
        try {
            log.info("获取报表仪表盘数据: startDate={}, endDate={}, limit={}", startDate, endDate, limit);
            Map<String, Object> dashboard = reportService.getReportDashboard(startDate, endDate, limit);
            return Result.success(dashboard);
        } catch (Exception e) {
            log.error("获取报表仪表盘数据失败", e);
            return Result.error("获取报表仪表盘数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取数据库表信息（仅用于调试）
     */
    @GetMapping("/db-info")
    public Result<List<String>> getDatabaseInfo() {
        try {
            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
            List<String> tables = jdbcTemplate.queryForList("SHOW TABLES", String.class);
            return Result.success(tables);
        } catch (Exception e) {
            log.error("获取数据库信息失败", e);
            return Result.error("获取数据库信息失败: " + e.getMessage());
        }
    }
} 